package com.ruoyi.web.controller.Ca;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.Docker;
import com.ruoyi.web.service.DockerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Api(tags = "医师接口")
@RestController
@RequestMapping("/physician")
public class DockerLoginController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DockerService dockerService;

    @ApiOperation(value = "医师注册")
    @PostMapping("/register")
    public AjaxResult physicianRegister(@RequestBody Docker docker){

        String name = docker.getName();
        String phonenumber = docker.getPhonenumber();
        String password = docker.getPassword();
        //判断用户名,手机号,密码是否为空
        if (StringUtils.isEmpty(name)){
            return AjaxResult.error("请输入用户名");
        }
        if (StringUtils.isEmpty(phonenumber)){
            return AjaxResult.error("请输入手机号");
        }
        if (StringUtils.isEmpty("password")){
            return AjaxResult.error("请输入密码");
        }
        //根据手机号查询,判断手机号是否被注册
        Docker dockerfindByPhoneNumber = dockerService.selectDockerByPhoneNumber(phonenumber);
        String phonenumber1 = dockerfindByPhoneNumber.getPhonenumber();
        if (phonenumber1.equals(phonenumber)){
            return AjaxResult.error("手机号已被注册");
        }
        int i = dockerService.insertDocker(docker);

        return AjaxResult.success("注册成功");
    }

    @ApiOperation(value = "根据手机号查找")
    @GetMapping("/findByNumber/{phonenumber}")
    public AjaxResult findByNumber(@PathVariable String phonenumber){
        //根据手机号查询
        Docker docker = dockerService.selectDockerByPhoneNumber(phonenumber);
        docker.setPassword("*****************");
        return AjaxResult.success(docker);
    }

    @ApiOperation(value = "根据手机号发送验证码")
    @PostMapping("/sendCaptcha/{phoneNumber}")
    public AjaxResult sendCaptcha(@PathVariable String phoneNumber){

        //TODO 验证手机号格式

        //生成四位数验证码
        double d = (Math.random() + 1) * 1000;
        String s = d + "";
        String captcha = s.substring(0,4);
        //将验证码放入缓存
        redisTemplate.opsForValue().set(phoneNumber,captcha,300, TimeUnit.SECONDS);
        System.out.println(captcha);

        //TODO 重新发送验证码

        //TODO 发送验证码到手机

        return AjaxResult.success(captcha);
    }

    @ApiOperation(value = "医师登录,手机号发送验证码登录")
    @GetMapping("/loginLoginByPhoneNumber/{phoneNumber}/{code}")
    public AjaxResult LoginByPhoneNumber(@PathVariable String phoneNumber,@PathVariable String code){
        //根据手机号获取验证码
        String captcha = (String) redisTemplate.opsForValue().get(phoneNumber);
        if (StringUtils.isEmpty(captcha)){
            return AjaxResult.error("请先发送验证码");
        }
        if (!code.equals(captcha)){
            return AjaxResult.error("验证码错误,请重新输入");
        }

        //根据手机号查询数据库,是否注册过
        Docker docker = dockerService.selectDockerByPhoneNumber(phoneNumber);

        if (docker == null){
            return AjaxResult.error("此手机号未注册,请先注册");
        }

        //生成token返回到客户端
        String token = dockerService.createToken(docker);
        return AjaxResult.success("登录成功").put(Constants.TOKEN,token);
    }

    @ApiOperation(value = "根据医师账号密码登录,账号为手机号")
    @PostMapping("/loginByAccount")
    public AjaxResult loginByAccount(@RequestBody Docker docker){
        //验证账号密码
        boolean b = dockerService.verification(docker);
        if (!b){
            return AjaxResult.error("账号或密码错误");
        }
        //生成token返回到客户端
        String token = dockerService.createToken(docker);
        return AjaxResult.success("登录成功").put(Constants.TOKEN,token);
    }


}
